package com.fuhailiu.opengl.renders;

import android.content.Context;
import android.view.MotionEvent;

import com.fuhailiu.opengl.utils.LogUtil;
import com.fuhailiu.opengl.view.GLView;
import com.fuhailiu.opengl.view.GLViewGroup;
import com.fuhailiu.opengl.widget.GLSixPointedStar;

public class RenderSixPointedStar extends AbsRender {

	private final String TAG = getClass().getSimpleName();

	public RenderSixPointedStar(Context context) {
		super(context);
	}

	@Override
	protected float[] getEyePosition() {
		return new float[] { 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f };
	}

	@Override
	protected float[] getProjectPosition(float ratio) {
		return new float[] { -ratio, ratio, -1, 1, 1.0f, 5.0f };
	}

	@Override
	protected boolean useOrtho() {
		return false;
	}

	@Override
	protected GLViewGroup makeWorld(Context context) {
		LogUtil.LogD(TAG, "makeWorld   <---");

		GLViewGroup layout = new GLViewGroup(context);

		final int number = 6;
		for (int i = 0; i < number; i++) {
			GLView sixPointedStar = new GLSixPointedStar(context, 1.0f, 0.4f, -0.3f * i);
			layout.addView(sixPointedStar);
		}

		LogUtil.LogD(TAG, "makeWorld   --->");

		return layout;
	}

	// 角度缩放比例
	private final float TOUCH_SCALE_FACTOR = 180.0f / 320;

	// 最后一次触摸点坐标
	private float mLastTouchX;
	private float mLastTouchY;

	@Override
	public void onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			break;

		case MotionEvent.ACTION_MOVE:
			float deltaX = x - mLastTouchX;
			float deltaY = y - mLastTouchY;
			int count = mGLViewGroup.getChildCount();
			for (int i = 0; i < count; i++) {
				GLView child = mGLViewGroup.getChildAt(i);
				if (child instanceof GLSixPointedStar) {
					GLSixPointedStar sixPointedStar = (GLSixPointedStar) child;
					sixPointedStar.mAngleX += deltaY * TOUCH_SCALE_FACTOR;
					sixPointedStar.mAngleY += deltaX * TOUCH_SCALE_FACTOR;
					LogUtil.LogD(TAG, "onTouchEvent angleX = " + sixPointedStar.mAngleX + ", angleY = " + sixPointedStar.mAngleY);
				}
			}
			break;

		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			break;
		}

		mLastTouchX = x;
		mLastTouchY = y;
	}

}
